package cn.wolfcode.util;

import cn.wolfcode.domain.Employee;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

//工具类
public class UserContext {
    //存放两个常量,一个是用于登录拦截器使用的员工对象的key  一个是用于权限拦截器使用的表达式列表的key
    public static final String EMPLOYEE_IN_SESSION="EMPLOYEE_IN_SESSION";
    public static final String EXPRESSION_IN_SESSION="EXPRESSION_IN_SESSION";

    //获取session对象
    public static HttpSession getSession(){
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        return request.getSession();
    }
    //往session中存入登录用户
    public static void setCurrentUser(Employee emp){
        HttpSession session = getSession();
        session.setAttribute(EMPLOYEE_IN_SESSION, emp);
    }
    //取出session中的登录用户
    public static Employee getCurrentUser(){
        HttpSession session = getSession();
        Employee employee = (Employee)session.getAttribute(EMPLOYEE_IN_SESSION);
        return employee;
    }
    //往session中存入用户权限信息
    public static void setExpression(List<String> permissions){
        getSession().setAttribute(EXPRESSION_IN_SESSION, permissions);
    }
    //从session中获取用户登录的权限信息
    public static List<String> getExpression(){
        List<String> expressions = (List<String>)getSession().getAttribute(EXPRESSION_IN_SESSION);
        return expressions;
    }
}
